﻿
//参考[将Git版本号编译进程序](https://blog.csdn.net/farsight1/article/details/81279654)
#include "version.h"
#include "untility.h"
#include "errorcode.h"
#include "help.h"
#include <ctime>


int main(int argc, char ** argv)
{
   // _wsetlocale(LC_ALL, L"chs");

    bool help = false;
    
    xparam pm;
    std::wstring tmplPath;
    std::vector<OutFile> outFiles;
    xint flag = 0;
    xint tortoise = 0;
    xint hasparams = 0;
    xint rcode = x_parse_cmdline(argc, argv, help, pm.codePath, tmplPath, outFiles, pm.gH, flag, tortoise, hasparams);
    if (0 == hasparams) {
        std::cout 
            << "gsrev " 
            << GS_VER_F_RESEV
            << "." 
            << GS_VER_F_MAJOR
            << "."
            << GS_VER_F_MINOR
            << "."
            << GS_VER_F_RELES
            << " Copyright (C) 2024~2024 xfowks, MIT"
            << std::endl;
        return 0;
    }
    if (help) {
        x_help();
        return 0;
    }
    if (0 != rcode)
    {
        std::cout << "命令解析错误, 请使用\"gsrev -h\"获取使用说明." << std::endl;
#ifdef _DEBUG
        system("pause");
#endif // _DEBUG
        return 0;
    }

    std::string buildTime;
    {
        char buffer[256];
        time_t rawtime;
        time(&rawtime);
        std::tm tm;
#ifdef GS_PLAT_WIN
        _localtime64_s(&tm, &rawtime);
#elif defined(GS_PLAT_LINUX)
        //struct tm tm;
        localtime_r(&rawtime, &tm);
#else
#error  Not supported: local time.
#endif
        strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
        buildTime = buffer;
    }

    xcodeinfo gitInfo;
    gitInfo.name = "git";
    if (0x1 == (0x1 & flag)) 
        x_code_git_info(pm, gitInfo);

    if (1 == gitInfo.result)
    {// 获取git信息成功
        std::cout << "$GSBuildDate$   : " << buildTime << std::endl;
        std::cout << "$GSCodeVerMgr$  : " << "git" << std::endl;
        std::cout << "$GSCodePath$    : " << gitInfo.localPath << std::endl;
        std::cout << "$GSCodeUrl$     : " << gitInfo.codePath << std::endl;
        std::cout << "$GSRevTxt$      : " << gitInfo.revTxt << std::endl;
        std::cout << "$GSRevVal$      : " << gitInfo.revVal << std::endl;
        std::cout << "$GSDate$        : " << gitInfo.datetime << std::endl;

        if (1 == tortoise)
        {
            std::cout << "$WCURL$     : " << gitInfo.codePath << std::endl;
            std::cout << "$WCREV$     : " << gitInfo.revVal <<  std::endl;
            std::cout << "$WCDATE$    : " << gitInfo.datetime << std::endl;
        }
    }

    xcodeinfo svnInfo;
    svnInfo.name = "svn";
    if (0x2 == (0x2 & flag))
        x_code_svn_info(pm, svnInfo);

    if (1 == svnInfo.result)
    {// 获取svn信息成功
        std::cout << "$GSBuildDate$  : " << buildTime << std::endl;
        std::cout << "$GSCodeVerMgr$ : " << "svn" << std::endl;
        std::cout << "$GSCodePath$   : " << svnInfo.localPath << std::endl;
        std::cout << "$GSCodeUrl$    : " << svnInfo.codePath << std::endl;// << std::endl;
        std::cout << "$GSRevTxt$     : " << svnInfo.revTxt << std::endl;
        std::cout << "$GSRevVal$     : " << svnInfo.revVal << std::endl;
        std::cout << "$GSDate$       : " << svnInfo.datetime << std::endl;

        if (1 == tortoise)
        {
            std::cout << "$WCURL$    : " << gitInfo.codePath << std::endl;
            std::cout << "$WCREV$    : " << gitInfo.revVal << std::endl;
            std::cout << "$WCDATE$   : " << gitInfo.datetime << std::endl;
        }
    }

    FILE *flTmp = xnullptr;
    {
        xchar opm[10] = { L'r', L't', '\0' };
        flTmp = x_wfsopen(tmplPath.c_str(), opm, 0x40/*_SH_DENYNO*/);
        if (xnullptr == flTmp)
        {
            std::wcout << L"打开模板文件失败!" << std::endl;
            return gsrev::ErrCode_FailedToOpenTemplate;
        }
    }

    xint cnt = 0;
    for (const auto &var : outFiles)
    {
        switch (var.outType)
        {
        case OutType_Git:
        {
            if (1 != gitInfo.result)
                break;

            if (x_out_file(flTmp, gitInfo, var.outFile, buildTime, tortoise))
                ++cnt;

            break;
        }
        case OutType_Svn:
        {
            if (1 != svnInfo.result)
                break;

            if (x_out_file(flTmp, svnInfo, var.outFile, buildTime, tortoise))
                ++cnt;

            break;
        }
        case OutType_GitSvn:
        {
            if (1 == gitInfo.result)
            {
                if (x_out_file(flTmp, gitInfo, var.outFile, buildTime, tortoise))
                    ++cnt;
            }
            else if (1 == svnInfo.result)
            {
                if (x_out_file(flTmp, svnInfo, var.outFile, buildTime, tortoise))
                    ++cnt;
            }

            break;
        }
        case OutType_SvnGit:
        {
            if (1 == svnInfo.result)
            {
                if (x_out_file(flTmp, svnInfo, var.outFile, buildTime, tortoise))
                    ++cnt;
            }
            else if (1 == gitInfo.result)
            {
                if (x_out_file(flTmp, gitInfo, var.outFile, buildTime, tortoise))
                    ++cnt;
            }

            break;
        }
        default:
            break;
        }
    }


    fclose(flTmp);

    if (outFiles.size() != cnt)
        std::cout << "未生成所有的目标文件, 请使用\"gsrev -h\"获取使用说明." << std::endl;

#ifdef _DEBUG
    system("pause");
#endif //_DEBUG
    return 0;
}